/************************************************************************/
/* GUI                                                                  */
/************************************************************************/
#include "GUI/Bars/ProgressBar.h"

namespace TD_GUI {


	/************************************************************************/
	/* Default definitions                                                  */
	/************************************************************************/
	const unsigned int ProgressBar::backgroundOffset = 2; //2 pixels
	const float ProgressBar::defaultProgressSpeed = 0.25/60.0;

	ProgressBar::ProgressBar() {

	}


	ProgressBar::ProgressBar(const ProgressBar &rhs) {

	}

	ProgressBar & ProgressBar::operator =(const ProgressBar & rhs) {
		return(*this);
	}

	ProgressBar::ProgressBar(const Coord2D & barPosition, const Coord2D & barSize, float progressSpeed,
		Color foregroundColor /* = Color::GREEN */, Color backgroundColor /* = Color::BLACK */) : BaseEntity(barPosition) {

		m_size = barSize;
		m_shapes.push_back(new RectangleShape(barPosition, barSize));
		m_foregroundColor = foregroundColor;
		m_backgroundColor = backgroundColor;
		m_progressSpeed = progressSpeed;
		m_foregroundRect.w = 0;
		m_error = 0.0f;

		m_isFull = false;
	}
	ProgressBar::~ProgressBar() {

	}

	void ProgressBar::update(const unsigned int deltaTime) {
		if(m_foregroundRect.w < m_backgroundRect.w - backgroundOffset) {
			m_error += m_progressSpeed;
			if(m_error > 1) {
				++m_foregroundRect.w;
				m_error = 0;
			}
		} else {
			m_isFull = true;
		}
	}

	void ProgressBar::restartProgress() {
		m_foregroundRect.x = 0;
		m_error = 0.0f;
		m_isFull = false;
	}

	void ProgressBar::render(SDL_Surface *const g_destSurface, unsigned int deltaTime) {
		m_backgroundRect.x = m_position.x;
		m_backgroundRect.y = m_position.y;

		m_backgroundRect.w = m_size.x;
		m_backgroundRect.h = m_size.y;

		m_foregroundRect.x = m_position.x + backgroundOffset;
		m_foregroundRect.y = m_position.y + backgroundOffset;

		update(deltaTime);
		m_foregroundRect.h = m_size.y - backgroundOffset;
	
		SDL_FillRect(g_destSurface, &m_backgroundRect, SDL_MapRGB(g_destSurface->format, m_backgroundColor.red, m_backgroundColor.green, m_backgroundColor.blue));
		SDL_FillRect(g_destSurface, &m_foregroundRect, SDL_MapRGB(g_destSurface->format, m_foregroundColor.red, m_foregroundColor.green, m_foregroundColor.blue));	
	}
}